using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ShootThemUp.Sources.GameElements.Ships.Ennemies.Waves.Trajectories;
using ShootThemUp.Sources.GameElements.Ships.Ennemies.Fruits;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using System.Diagnostics;
using ShootThemUp.Sources.GameElements.Animations;

namespace ShootThemUp.Sources.GameElements.Ships.Ennemies.Waves
{
    public class Wave
    {
        /// <summary>
        /// The list of the fruits, ie the ennemies
        /// </summary>
        private List<Fruit> fruits;
        /// <summary>
        /// The trajectory of all the fruits in the list of fruits.
        /// </summary>
        private IWaveTrajectory waveTrajectory; //TODO : Instanciate

        private int time;

        private int offsetTime;

        private float speed;

        private int positionIndex = 0;
        
        public IWaveTrajectory WaveTrajectory
        {
            get { return waveTrajectory; }
            set { waveTrajectory = value; }
        }

        public List<Fruit> Fruits
        {
            get { return fruits; }
        }

        public int Time
        {
            get { return time; }
            set { time = value; }
        }

        public int OffsetTime
        {
            get { return offsetTime; }
            set { offsetTime = value; }
        }


        public Wave(IWaveTrajectory trajectory,int time, int offsetTime, float speed)
        {
            this.WaveTrajectory = trajectory;
            this.fruits = new List<Fruit>();
            this.Time = time;
            this.OffsetTime = offsetTime;
            this.speed = speed;
        }

        public void Add(Fruit fruit)
        {
            fruit.PositionInTheWave = this.positionIndex;
            this.positionIndex++;
            this.Fruits.Add(fruit);
        }

        public void LoadContent(ContentManager content, string assetName)
        {
            foreach (Fruit fruits in this.fruits)
                fruits.LoadContent(content, assetName);
        }

        public void Draw(SpriteBatch spriteBatch,int totalTime){
            int t;
            int position;

            foreach(Fruit fruit in this.Fruits)
            {
                position = fruit.PositionInTheWave;

                t = totalTime - (this.time + position * this.offsetTime);

                if (t >= 0)
                {
                    fruit.Draw(spriteBatch);
                }

            }
        }

        public List<Animation> Update(int totalTime, GameTime gameTime)
        {
            int t;
            int position;
            List<Animation> animations = new List<Animation>();

            foreach (Fruit fruit in this.Fruits)
            {
                position = fruit.PositionInTheWave;

                t = totalTime - (this.time + position * this.offsetTime);
        
                if (t >= 0)
                {
                    fruit.Position = new Vector2(this.WaveTrajectory.GetX(t*speed), this.WaveTrajectory.GetY(t*speed));

                    if (!fruit.Spawn && !(this.WaveTrajectory is HorizontalTrajectory))
                    {
                        TeleportationEffect te = new TeleportationEffect(fruit.Position);
                        te.Scale = new Vector2(2,2);
                        animations.Add(te);
                        fruit.Spawn = true;
                    }
                    fruit.Update(gameTime);
                }
            }

            return animations;
        }
        

    }
}
